import net.minidev.json.{JSONArray, JSONObject, JSONValue}

object json {

  def main(args: Array[String]): Unit = {
    val lines = scala.io.Source.fromFile("/home/lzt/tmp/json.txt").getLines().flatMap(row => {
      val json = JSONValue.parse(row).asInstanceOf[JSONObject]
      json.get("result").asInstanceOf[JSONArray].toArray
          .filter(x => {
            x.asInstanceOf[JSONObject].containsKey("hphm")
          }).map(x => {
        val j = x.asInstanceOf[JSONObject]
        (j.get("kcbh").asInstanceOf[String], j.get("hphm").asInstanceOf[String])
      })
    })
    lines.foreach(println)
  }


  def test1(): Unit = {
    val s = "['log-request-time']\t{\"head\":{\"_url\":\"/bgpt/ajax.do\",\"_ip\":\"192.168.1.197\",\"_user_agent\":\"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36\",\"username\":\"\",\"_type\":\"ajax\",\"_target_url\":\"\",\"_begin_url\":\"\",\"service_code\":\"P11000\",\"send_time\":\"2018-03-19 09:42:24\",\"_runTime\":38},\"session\":\"org.apache.catalina.session.StandardSessionFacade@4a23e861\",\"password\":\"613613613\",\"username\":\"000134\",\"ipt_session_name\":{},\"_runTime\":33}"
    val json = s.substring(21)
    println(s"json string : ${json}")
    val obj = JSONValue.parse(json)
    println(s"object :  ${obj}")
    val jsonObj = obj.asInstanceOf[JSONObject]
    println(s"run time : ${jsonObj.get("_runTime").asInstanceOf[String]}")
  }

  def test2(): Unit = {

  }
}
